# Blocking I/O & Non-Blocking I/O


I/O ์ž‘์—…์€ Kernel level์—์„œ๋งŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ, Process, Thread๋Š” ์ปค๋„์—๊ฒŒ I/O๋ฅผ ์š”์ฒญํ•ด์•ผ ํ•œ๋‹ค.


  1. # Blocking I/O

    I/O Blocking ํ˜•ํƒœ์˜ ์ž‘์—…์€

    (1) Process(Thread)๊ฐ€ Kernel์—๊ฒŒ I/O๋ฅผ ์š”์ฒญํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ

    (2) Kernel์ด ์ž‘์—…์„ ์™„๋ฃŒํ•˜๋ฉด ์ž‘์—… ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ ๋ฐ›์Œ.

    • ํŠน์ง•
      • I/O ์ž‘์—…์ด ์ง„ํ–‰๋˜๋Š” ๋™์•ˆ user Process(Thread) ๋Š” ์ž์‹ ์˜ ์ž‘์—…์„ ์ค‘๋‹จํ•œ ์ฑ„ ๋Œ€๊ธฐ
      • Resource ๋‚ญ๋น„๊ฐ€ ์‹ฌํ•จ
        (I/O ์ž‘์—…์ด CPU ์ž์›์„ ๊ฑฐ์˜ ์“ฐ์ง€ ์•Š์œผ๋ฏ€๋กœ)

    ์—ฌ๋Ÿฌ Client ๊ฐ€ ์ ‘์†ํ•˜๋Š” ์„œ๋ฒ„๋ฅผ Blocking ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒฝ์šฐ -> I/O ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๋Š” ์ž‘์—…์„ ์ค‘์ง€ -> ๋‹ค๋ฅธ Client๊ฐ€ ์ง„ํ–‰์ค‘์ธ ์ž‘์—…์„ ์ค‘์ง€ํ•˜๋ฉด ์•ˆ๋˜๋ฏ€๋กœ, client ๋ณ„๋กœ ๋ณ„๋„์˜ Thread๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•จ -> ์ ‘์†์ž ์ˆ˜๊ฐ€ ๋งค์šฐ ๋งŽ์•„์ง

    ์ด๋กœ ์ธํ•ด, ๋งŽ์•„์ง„ Threads ๋กœ ์ปจํ…์ŠคํŠธ ์Šค์œ„์นญ ํšŸ์ˆ˜๊ฐ€ ์ฆ๊ฐ€ํ•จ,,, ๋น„ํšจ์œจ์ ์ธ ๋™์ž‘ ๋ฐฉ์‹


  2. # Non-Blocking I/O

    I/O ์ž‘์—…์ด ์ง„ํ–‰๋˜๋Š” ๋™์•ˆ User Process์˜ ์ž‘์—…์„ ์ค‘๋‹จํ•˜์ง€ ์•Š์Œ.

    • ์ง„ํ–‰ ์ˆœ์„œ

      1. User Process๊ฐ€ recvfrom ํ•จ์ˆ˜ ํ˜ธ์ถœ (์ปค๋„์—๊ฒŒ ํ•ด๋‹น Socket์œผ๋กœ๋ถ€ํ„ฐ data๋ฅผ ๋ฐ›๊ณ  ์‹ถ๋‹ค๊ณ  ์š”์ฒญํ•จ)

      2. Kernel์€ ์ด ์š”์ฒญ์— ๋Œ€ํ•ด์„œ, ๊ณง๋ฐ”๋กœ recvBuffer๋ฅผ ์ฑ„์›Œ์„œ ๋ณด๋‚ด์ง€ ๋ชปํ•˜๋ฏ€๋กœ, "EWOULDBLOCK"์„ returnํ•จ.

      3. Blocking ๋ฐฉ์‹๊ณผ ๋‹ฌ๋ฆฌ, User Process๋Š” ๋‹ค๋ฅธ ์ž‘์—…์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์Œ.

      4. recvBuffer์— user๊ฐ€ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ, Buffer๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ ๋ฐ›์•„์˜ด.

        ์ด๋•Œ, recvBuffer๋Š” Kernel์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฉ”๋ชจ๋ฆฌ์— ์ ์žฌ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ, Memory๊ฐ„ ๋ณต์‚ฌ๋กœ ์ธํ•ด, I/O๋ณด๋‹ค ํ›จ์”ฌ ๋น ๋ฅธ ์†๋„๋กœ data๋ฅผ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ์Œ.

      5. recvfrom ํ•จ์ˆ˜๋Š” ๋น ๋ฅธ ์†๋„๋กœ data๋ฅผ ๋ณต์‚ฌํ•œ ํ›„, ๋ณต์‚ฌํ•œ data์˜ ๊ธธ์ด์™€ ํ•จ๊ป˜ ๋ฐ˜ํ™˜ํ•จ.

์ตœ์ข… ์ˆ˜์ • : 12/17/2022, 7:23:59 AM